package com.project.service.impl;

import com.project.bean.ClassRoomBean;
import com.project.mapper.IClassMapper;
import com.project.mapper.IStudentMapper;
import com.project.service.IClassService;
import org.apache.ibatis.session.SqlSession;

import java.time.LocalDate;
import java.util.List;

public class ClassServiceImpl extends BaseService implements IClassService {

    @Override
    public void add(ClassRoomBean classroom) {
        SqlSession session = this.getSession();
        IClassMapper mapper = session.getMapper(IClassMapper.class);
        mapper.addClass(classroom);
        session.commit();
        session.close();
    }

    @Override
    public boolean del(int id) {
        SqlSession session = this.getSession();
        IStudentMapper mapper = session.getMapper(IStudentMapper.class);
        if(mapper.countStudentByClass(id) == 0){
            IClassMapper mapper1 = session.getMapper(IClassMapper.class);
            mapper1.delClass(id);
            session.commit();
            session.close();
            return true;
        }
        session.close();
        return false;
    }

    @Override
    public List<ClassRoomBean> findAll() {
        SqlSession session = this.getSession();
        IClassMapper mapper = session.getMapper(IClassMapper.class);
        List<ClassRoomBean> list = mapper.findAll();
        session.close();
        return list;
    }

    public static void main(String[] args) {
        IClassService service = new ClassServiceImpl();
//        service.add(new ClassRoomBean("GT12","洪安通", LocalDate.parse("2012-09-18")));
//        System.out.println(service.del(1));
        System.out.println(service.findAll());

    }
}
